﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.DAL;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.BLL.Service
{
    public class TravelVoService : BaseRepository<Travelvo>, ITravelVoService
    {
        /// <summary>
        /// 获取旅游卷集合
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult GetTravelVoList(int page, int limit)
        {
            ResponseResult responseResult = new ResponseResult();
            //创建实例            
            using (var db = new SyDbContext())
            {
                //两表连接查询
                var result = from a in db.Travelvo
                             join b in db.Travelvo_record
                             on a.TravelVo_Id equals b.TravelVo_Id
                             select new TravelVoInfo() //自定义实体类 包括需要展示的字段
                             {
                                 //赋值
                                 TravelVoRecord_Id = b.TravelVoRecord_Id,
                                 User_Id = a.User_Id,
                                 TravelVo_Id = a.TravelVo_Id,
                                 TravelVoRecord_Money = b.TravelVoRecord_Money,
                                 TravelVoRecord_OperationMethod = b.TravelVoRecord_OperationMethod,
                                 TravelVoRecord_Effective = b.TravelVoRecord_Effective,
                                 TravelVoRecord_CreateTime = b.TravelVoRecord_CreateTime
                             };
                //responseResult对象赋值
                responseResult.data = result.ToList().Skip((page - 1) * limit).Take(limit);
                responseResult.count = result.ToList().Count;
                responseResult.msg = string.Empty;
                responseResult.code = 0;
            }
            return responseResult;
        }



        /// <summary>
        /// 删除旅游卷操作记录
        /// </summary>
        /// <param name="voucher_Record"></param>
        /// <returns></returns>
        public ResponseResult DeleteRecord(Travelvo_record travelvo_Record)
        {
            ResponseResult responseResult = new ResponseResult();
            using (var db = new SyDbContext())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除
                        db.Entry(travelvo_Record).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        responseResult.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        //throw ex.InnerException;
                        responseResult.Success = false;
                    }
                }

            }
            return responseResult;
        }


        /// <summary>
        /// 删除旅游卷和操作记录
        /// </summary>
        /// <param name="voucher_Record"></param>
        /// <returns></returns>
        public ResponseResult DelAllTravelVo(Travelvo travelvo)
        {
            ResponseResult responseResult = new ResponseResult();
            using (var db = new SyDbContext())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        List<Travelvo_record> list = db.Travelvo_record.Where(a => a.TravelVo_Id == travelvo.TravelVo_Id).ToList();
                        //删除
                        for (int i = 0; i < list.Count; i++)
                        {
                            var entity = db.Travelvo_record.Find(list[i].TravelVoRecord_Id);
                            if (entity != null)
                            {
                                db.Travelvo_record.Remove(entity);
                            }
                        }
                        //删除抵用卷
                        db.Entry(travelvo).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        responseResult.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        //throw ex.InnerException;
                        responseResult.Success = false;
                    }
                }
            }
            return responseResult;
        }

        /// <summary>
        /// 发送旅游卷并添加添加记录
        /// </summary>
        /// <param name="voucher_Record"></param>
        /// <returns></returns>
        public ResponseResult AddTravelVo(Travelvo travelvo)
        {
            ResponseResult responseResult = new ResponseResult();
            using (var db = new SyDbContext())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {

                        //删除
                        db.Travelvo.Add(travelvo);
                        //保存
                        db.SaveChanges();

                        //创建操作记录对象
                        Travelvo_record travelvo_Record = new Travelvo_record();
                        travelvo_Record.TravelVo_Id = travelvo.TravelVo_Id;
                        travelvo_Record.TravelVoRecord_Money = travelvo.TravelVo_balance;
                        travelvo_Record.TravelVoRecord_OperationMethod = "获取";
                        travelvo_Record.TravelVoRecord_CreateTime = DateTime.Now;
                        travelvo_Record.TravelVoRecord_Effective = DateTime.Now.AddMonths(1);
                        db.Travelvo_record.Add(travelvo_Record);
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        responseResult.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        //throw ex.InnerException;
                        responseResult.Success = false;
                    }
                }

            }
            return responseResult;
        }
    }
}
